<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
    </el-row>
    <el-table border stripev-loading="loading" :data="list">
      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="路径" align="center" prop="posterImage"/>
      <el-table-column label="缩略图" align="center">
        <template slot-scope="scope">
          <el-image style="width: 50px; height: 50px" :src="baseUrl + scope.row.posterImage" fit="fill"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog title="新增海报" :visible.sync="visible" width="500px" append-to-body>
      <div style="text-align: center;">
        <ImageUpload ref="imageUpload" :limit="1"/>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="visible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listPoster, getNotice, delPoster, addNotice, updateNotice, addPoster} from "@/api/poster";
import ImageUpload from "@/components/ImageUpload/index.vue";

export default {
  components: {ImageUpload},
  name: "quotation",
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      visible: false,
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 公告表格数据
      list: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    submitForm() {
      if (this.$refs.imageUpload.fileList.length === 0) {
        this.$modal.msgError("请上传海报图片");
        return;
      }
      let image = this.$refs.imageUpload.fileList[0].name
      addPoster({posterImage: image}).then(res => {
        this.visible = false;
        this.$modal.msgSuccess("上传成功");
        this.getList();
      })
    },
    handleAdd() {
      if(this.$refs.imageUpload && this.$refs.imageUpload.fileList){
        this.$refs.imageUpload.fileList = [];
      }
      this.visible = true;
    },
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      listPoster(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const noticeIds = row.id
      this.$modal.confirm('是否确认删除编号为"' + noticeIds + '"的海报？').then(function () {
        return delPoster(noticeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      })
    }
  }
};
</script>
